package com.syezon.wifikey.utils;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.syezon.android.base.utils.Utils;

/**
 * Created by Administrator on 2017/7/7.
 */

public class AppInfoUtil {

    public static final String KEY_META_CHANNEL = "UMENG_CHANNEL";
    public static String TAG = AppInfoUtil.class.getSimpleName();
    private static String VERSION_NAME = "";
    private static int VERSION_CODE = 0;
    private static String PACKAGE_NAME = "";
    private static String CHANNEL = "";
    private static String APP_NAME = "";


    /**
     * @return 获取应用版本名
     */
    public static String getAPPVersionName() {
        if (!TextUtils.isEmpty(VERSION_NAME)) {
            return VERSION_NAME;
        }
        try {
            PackageManager manager = Utils.getApp().getPackageManager();
            PackageInfo packageInfo = manager.getPackageInfo(Utils.getApp().getPackageName(), 0);
            VERSION_NAME = packageInfo.versionName;
        } catch (Exception e) {
            Log.i(TAG, "");
        }
        return VERSION_NAME;
    }

    public static int getAPPVersionCode() {
        if (VERSION_CODE > 0) {
            return VERSION_CODE;
        }
        try {
            PackageManager manager = Utils.getApp().getPackageManager();
            PackageInfo packageInfo = manager.getPackageInfo(Utils.getApp().getPackageName(), 0);
            VERSION_CODE = packageInfo.versionCode;
        } catch (Exception e) {
            Log.i(TAG, "");
        }
        return VERSION_CODE;
    }

    public static String getAPPChannel() {
        if (!TextUtils.isEmpty(CHANNEL)) {
            return CHANNEL;
        }
        try {
            PackageManager manager = Utils.getApp().getPackageManager();
            ApplicationInfo application = manager.getApplicationInfo(Utils.getApp().getPackageName(), PackageManager.GET_META_DATA);
            CHANNEL = application.metaData.getString(KEY_META_CHANNEL, "");
        } catch (Exception e) {
            Log.i(TAG, "");
        }
        return CHANNEL;
    }

    public static String getAppName() {
        if (!TextUtils.isEmpty(APP_NAME)) {
            return APP_NAME;
        }
        try {
            PackageManager packageManager = Utils.getApp().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    Utils.getApp().getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            APP_NAME = Utils.getApp().getResources().getString(labelRes);
        } catch (Exception e) {

        }
        return APP_NAME;
    }

}
